package com.fishingwithme.controllers;

import com.fishingwithme.domain.spots.SpotType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.StorageClient1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/public")
@Tag(name = "Public APIs", description = "Public APIs that don't require authentication")
public class PubController {
    @Autowired
    private StorageClient1 storageClient;

    @GetMapping("/enum")
    @Operation(
            summary = "Get spot type enum",
            description = "Retrieve a sample spot type enum value"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "Spot type enum retrieved successfully",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = SpotType.class)
                    )
            )
    })
    public SpotType getEnum() {
        return SpotType.CAODONG;
    }

    @GetMapping("/test")
    @Operation(
            summary = "Test file storage",
            description = "Test endpoint for FastDFS file upload and download functionality"
    )
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "File operation completed successfully",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(type = "string")
                    )
            ),
            @ApiResponse(
                    responseCode = "500",
                    description = "File operation failed"
            )
    })
    public @ResponseBody String test() throws MyException, IOException {
        try {

            NameValuePair[] metaList = new NameValuePair[2];
            metaList[0] = new NameValuePair("author", "doubao");
            metaList[1] = new NameValuePair("description", "测试文件");

            String fileId = storageClient.upload_file1("D:\\log.txt", null, metaList);
            storageClient.download_file1(fileId, "d:\\log_download.txt");
            return fileId;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
